<?php
/**
 * Tamplate:
 *
 * - 'all' => '****'
 * - 'none' => 'cover_page'
 * - '3' => '***er_page'
 * - '3 begin' => '***er_page'
 * - '3 begin none' => 'cov****'
 * - '3 end' => 'cover_p***'
 * - '3 end none' => '****age'
 * - '3 begin 2 end' => '***er_pa**'
 * - '3 begin 2 end none' => 'cov****ge'
 *
 * @author Roman Shuplov
 */
class mask_var {


    /**
     * @uses mask_var_class::_begin()
     * @uses mask_var_class::_end()
     * @uses mask_var_class::_2size()
     * @uses mask_var_class::_asterisk4()
     *
     * @param string $data
     * @param string $mask
     * @return string
     */
    public static function mask($data, $mask){

        if ( $mask === 'all' ) { // all open
            return self::_asterisk4();
        }
        if ( $mask === 'none' ) { // all close
            return $data;
        }


        $strlen_mask = strlen($mask);
        if ( 0 < $strlen_mask && 2 >= $strlen_mask ) { // If just integer, close from the begining
            return self::_begin($data, (int)$mask);
        }


        $str_begin = ( strpos($mask, 'begin') ) ? true : false; // At begin
        $str_end   = ( strpos($mask, 'end') )   ? true : false; // At end
        $reverse   = ( strpos($mask, 'none') )  ? true : false; // Reverse


        if ( $str_begin && $str_end ) {
            return self::_2size($data, $mask, $reverse);
        }
        if ( $str_begin ) {
            return self::_begin($data, (int)$mask, $reverse);
        }
        if ( $str_end ) {
            return self::_end($data, (int)$mask, $reverse);
        }


    }




    /**
     * 
     *
     * @param string $data
     * @param int $asterisk_count
     * @param boolean $reverse
     * @return string
     */
    protected static function _begin($data, $asterisk_count, $reverse = false){
        if (!$reverse) {
            return str_repeat('*', $asterisk_count) . substr($data, $asterisk_count);
        }
        return substr($data, 0, $asterisk_count) . self::_asterisk4();
    }
    /**
     * 
     *
     * @param string $data
     * @param int $asterisk_count
     * @param boolean $reverse
     * @return string
     */
    protected static function _end($data, $asterisk_count, $reverse = false){
        if (!$reverse) {
            return substr($data, 0, (-1)*$asterisk_count) . str_repeat('*', $asterisk_count);
        }
        return self::_asterisk4() . substr($data, (-1)*$asterisk_count);
    }
    /**
     * 
     *
     * @param string $data
     * @param string $mask
     * @param boolean $reverse
     * @return string
     */
    protected static function _2size($data, $mask, $reverse = false){

        $array_mask = explode(' ', $mask);
        $asterisk_count1 = $array_mask[0];
        $asterisk_count2 = $array_mask[2];

        if (!$reverse) {
            return  str_repeat('*', $asterisk_count1) .
                    substr($data, $asterisk_count1, (-1)*$asterisk_count2) .
                    str_repeat('*', $asterisk_count2);
        }
        return  substr($data, 0, $asterisk_count1) .
                self::_asterisk4() .
                substr($data, (-1)*$asterisk_count2);
    }





    /**
     *
     * @return string ****
     */
    protected static function _asterisk4 (){
        return '****';
    }






}












